In this article, we will cover working with report templates using the Remote API. Executing reports through the SDK implies a sequence of mandatory actions that are not visible when working in the web interface. To complete the picture, examples of working with reports with and without grouping will be given.
Creating Reports
In most cases, users create report templates in the web interface and then execute them using API requests. We will be considering such cases later in this article.
However, you can create a report template through the SDK by using the request report/update_report.
Sequence of Actions
To obtain report results using the Remote API, several consecutive requests need to be sent. The use of some of them depends on the presence of grouping in the report and the amount of analyzed data. Here is the general list of actions:
Authorization through the request token/login and setting localization through the request render/set_locale.
- Obtaining the necessary system IDs for:
the object the report needs to be executed for;
- the resource that contains the report template;
- the report template itself.
Executing the report through the request report/exec_report.
- Checking the report execution status through the request report/get_report_status.
After confirming the report is ready, you can proceed with following the instructions. - Obtaining the report result through the request report/apply_report_result.
The result of this request will provide the number of rows, columns, and levels of nesting if grouping is used. - Getting the table rows through the request report/get_result_rows for a report without grouping or through the request report/select_result_rows for a report with grouping.
The selection of the table, nesting levels, and displayed rows is based on the data from the previous response. - Exporting to a file through the request report/export_result.
This step is optional but quite simple and useful, so we will also consider it. - Deleting the result of the previous report through the request report/cleanup_result.
This step is mandatory if multiple reports must be executed in one session.
Next, we will consider two examples of working with reports (without grouping and with grouping).
Working with a Report without Grouping
Let’s imagine, it we need to obtain the results of the the report List of trips, available to the user authorized by a token, for the unit Truck 0769 (system ID 55555) for the time interval from 2023 June 30 20:00 to 2023 July 01 03:59 (GMT+0) and export it to a PDF file using API requests.
In the web interface, the result looks as follows:
1. Authorization and Localization
Use the request token/login.
https://hst-api.wialon.com/wialon/ajax.html?svc=token/login¶ms={"token":"TOKEN_VALUE"}
Authorization details are described in one of the previous articles.
Localization settings include setting the time zone (which has also been discussed earlier), date format, and other parameters.
Use the request render/set_locale.
https://hst-api.wialon.com/wialon/ajax.html?svc=render/set_locale¶ms={"tzOffset":134217728,"language":"en","formatDate":"%25E.%25m.%25Y%20%25H:%25M:%25S"}&sid=SESSION_IDENTIFIER
Parameter and its value | Description |
---|---|
"tzOffset":134217728 | GMT+0 time zone will be applied (without daylight saving time). |
"language":"en" | English language will be used. |
"formatDate":"%25Y.%25m.%25E%20%25H:%25M:%25S" | Date format will be yyyy-MM-dd and time format will be HH:mm:ss. |
2. Obtaining System IDs
In one of the previous articles, we have already described how to search items by property, so now we will focus only on searching a resource and report template.
Let’s get a list of all resources that have a report template named List of trips available to the user authorized by a token. Use the request core/search_items:
https://hst-api.wialon.com/wialon/ajax.html?svc=core/search_items¶ms={"spec":{"itemsType":"avl_resource","propType":"propitemname","propName":"reporttemplates","propValueMask":"List%20of%20trips","sortType":"sys_name"},"force":1,"flags":8193,"from":0,"to":0}&sid=SESSION_IDENTIFIER
Parameter and its value | Description |
---|---|
"itemsType":"avl_resource" | The search will be performed for resources. |
"propType":"propitemname" | The search will be performed based on the subitem name. |
"propName":"reporttemplates" | The search will be performed based on the report template name. |
"propValueMask":"List%20of%20trips" | The response will display a list of resources that have a report template named List of trips. |
"sortType":"sys_name" | The sorting will be performed based on the name of the item. |
"force":1 | Previous search results will not be taken into account. |
"flags":8193 | The response will contain information about the general properties and created report templates. 1 + 8192 = 8193 |
"from":0;"to":0 | There will be no restrictions on the number of found items. |
Pay attention to the following parameters in the response:
- nm — resource name;
rep — an array of report templates created in the resource;
- n — subitem name;
- id — system ID.
In this case, the user has access to the sdk_account resource with system ID 12345678, which contains the report template List of trips with system ID 1.
3. Executing the Report
Use the request report/exec_report.
https://hst-api.wialon.com/wialon/ajax.html?svc=report/exec_report¶ms={"reportResourceId":12345678,"reportTemplateId":1,"reportObjectId":55555,"reportObjectSecId":0,"interval":{"flags":0,"from":1688144400,"to":1688173199},"remoteExec":1}&sid=SESSION_IDENTIFIER
Parameter and its value | Description |
---|---|
"reportResourceId":12345678 | The report template will be selected from the resource with system ID 12345678. |
"reportTemplateId":1 | The report template with system ID 1 will be executed. |
"reportObjectId":55555 | The report will be executed for the object with system ID 55555. |
"reportObjectSecId":0 | The report will not be executed for a subitem. |
"flags":0 | The report will be executed for the specified interval. |
"from":1688144400 | The interval will start on 2023 June 30 at 20:00 (GMT+0). |
"to":1688173199 | The interval will end on 2023 July 1 at 03:59 (GMT+0). |
"remoteExec":1 | The report will be executed in the background on the server. |
4. Checking the Execution Status
Since the report is being executed in the background on the server, we will check its execution status using the request report/get_report_status.
https://hst-api.wialon.com/wialon/ajax.html?svc=report/get_report_status¶ms={}&sid=SESSION_IDENTIFIER
A status code 4 means that the report has been executed successfully. The interpretation of other values can be found on the page with a request description.
5. Obtaining the Report Result
Use the request report/apply_report_result.
https://hst-api.wialon.com/wialon/ajax.html?svc=report/apply_report_result¶ms={}&sid=SESSION_IDENTIFIER
Three key parameters from the response that are important for us here are as follows:
- tables — shows the number of tables in the report as an array. In this case, only one table (Trips) is mentioned, therefore its index will be 0.
- rows — the number of rows in the table, which is 4 in this case. Therefore, their indexes range from 0 to 3.
- level — the number of levels of nesting, which is 1 in this case, as there is no grouping in the report.
Other parameters from the response can also be briefly listed. The columns parameter indicates the number of columns, and their names are described in the header parameter. A value of 0 for the msgsRendered parameter indicates that the report template doesn't display tracks on the map. Empty stats and attachments parameters indicate that there are no Statistics nor attachments (e.g., charts) in the report template.
6. Getting Table Rows
Knowing the index of the table content, let's display it using the request report/get_result_rows.
https://hst-api.wialon.com/wialon/ajax.html?svc=report/get_result_rows¶ms={"tableIndex":0,"indexFrom":0,"indexTo":3}&sid=SESSION_IDENTIFIER
Parameter and its value | Description |
---|---|
"tableIndex":0 | The content of the table with index 0 will be displayed. |
"indexFrom":0 | The first displayed row will have index 0. |
"indexTo":3 | The last displayed row will have index 3. |
7. Export to File
Let’s export the result to a PDF file using the request report/export_result.
https://hst-api.wialon.com/wialon/ajax.html?svc=report/export_result¶ms={"format":2,"compress":0,"outputFileName":"List%20of%20trips"}&sid=SESSION_IDENTIFIER
Parameter and its value | Description |
---|---|
"format":2 | The report result will be exported in PDF format. |
"compress":0 | The exported file will not be compressed (added to an archive). |
"outputFileName":"List%20of%20trips" | The exported file will have the name List of trips. |
There is no response displayed for this API request, instead, the file download starts automatically.
8. Deleting the Result of the Previous Report
Use the request report/cleanup_result.
https://hst-api.wialon.com/wialon/ajax.html?svc=report/cleanup_result¶ms={}&sid=SESSION_IDENTIFIER
A zero value indicates successful deletion.
Working with a Report with Grouping
Now we need to obtain the results of the report Grouped trips, available to the user authorized by a token, for the unit Truck 0769 (system ID 55555) for the time interval from 2023 June 30 20:00 to 2023 July 01 03:59 (GMT+0) and export it to a PDF file using API requests.
In the web interface, the result looks as follows:
Based on the result in the web interface, you can see that the Trips table has grouping by months and dates, meaning the report has 3 levels of nesting:
- The first level contains rows with months.
The second level contains rows with dates within the month. - The third level contains rows with trips within the date.
1. Authorization and Localization
Use the request token/login.
https://hst-api.wialon.com/wialon/ajax.html?svc=token/login¶ms={"token":"TOKEN_VALUE"}
Authorization details are described in one of the previous articles.
Localization settings include setting the time zone (which has also been discussed earlier), date format, and other parameters.
Use the request render/set_locale.
https://hst-api.wialon.com/wialon/ajax.html?svc=render/set_locale¶ms={"tzOffset":134217728,"language":"en","formatDate":"%25E.%25m.%25Y%20%25H:%25M:%25S"}&sid=SESSION_IDENTIFIER
Parameter and its value | Description |
---|---|
"tzOffset":134217728 | GMT+0 time zone will be applied (without daylight saving time). |
"language":"en" | English language will be used. |
"formatDate":"%25Y.%25m.%25E%20%25H:%25M:%25S" | Date format will be yyyy-MM-dd and time format will be HH:mm:ss. |
2. Obtaining System IDs
In one of the previous articles, we have already described how to search items by property, so now we will focus only on searching a resource and report template.
Let’s get a list of all resources that have a report template named Grouped trips available to the user authorized by a token. Use the request core/search_items:
https://hst-api.wialon.com/wialon/ajax.html?svc=core/search_items¶ms={"spec":{"itemsType":"avl_resource","propType":"propitemname","propName":"reporttemplates","propValueMask":"Grouped%20trips","sortType":"sys_name"},"force":1,"flags":8193,"from":0,"to":0}&sid=SESSION_IDENTIFIER
Parameter and its value | Description |
---|---|
"itemsType":"avl_resource" | The search will be performed on resources. |
"propType":"propitemname" | The search will be performed based on the subitem name. |
"propName":"reporttemplates" | The search will be performed based on the report template name. |
"propValueMask":"Grouped%20trips" | The response will display a list of resources where a report template named Grouped trips is created. |
"sortType":"sys_name" | The sorting will be performed based on the name of the item. |
"force":1 | Previous search results will not be taken into account. |
"flags":8193 | The response will contain information about the general properties and created report templates. 1 + 8192 = 8193 |
"from":0;"to":0 | There will be no restrictions on the number of found items. |
Pay attention to the following parameters from the response:
- nm — resource name;
rep — an array of report templates created in the resource;
- n — subitem name;
- id — system ID.
In this case, the user has access to the sdk_account resource with system ID 12345678, which contains the report template Grouped trips with system ID 2.
3. Executing the Report
Use the request report/exec_report.
https://hst-api.wialon.com/wialon/ajax.html?svc=report/exec_report¶ms={"reportResourceId":12345678,"reportTemplateId":2,"reportObjectId":55555,"reportObjectSecId":0,"interval":{"flags":0,"from":1688144400,"to":1688173199},"remoteExec":1}&sid=SESSION_IDENTIFIER
Parameter and its value | Description |
---|---|
"reportResourceId":12345678 | The report template will be selected from the resource with system ID 12345678. |
"reportTemplateId":2 | The report template with system ID 2 will be executed. |
"reportObjectId":55555 | The report will be executed for the object with system ID 55555. |
"reportObjectSecId":0 | The report will not be executed for a subitem. |
"flags":0 | The report will be executed for the specified interval. |
"from":1688144400 | The interval will start on 2023 June 30 at 20:00 (GMT+0). |
"to":1688173199 | The interval will end on 2023 July 1 at 03:59 (GMT+0). |
"remoteExec":1 | The report will be executed in the background on the server. |
4. Checking the Execution Status
Since the report is being executed in the background on the server, let’s check the status of its execution using the request report/get_report_status.
https://hst-api.wialon.com/wialon/ajax.html?svc=report/get_report_status¶ms={}&sid=SESSION_IDENTIFIER
Status code 4 indicates that the report has been executed successfully. The interpretation of other values can be found on the page with a request description.
5. Obtaining the Report Result
Use the request report/apply_report_result.
https://hst-api.wialon.com/wialon/ajax.html?svc=report/apply_report_result¶ms={}&sid=SESSION_IDENTIFIER
Three key parameters from the response that are important for us here are as follows:
- tables — shows the number of tables in the report as an array. In this case, only one table (Trips) is mentioned, therefore its index will be 0.
- rows — the number of rows in the table, which is 2 in this case. Therefore, their indexes range from 0 to 1. This refers only to the rows at the top level of nesting, there may be more inside.
- level — the number of levels of nesting, which is 3 in this case, as there is grouping in the report. Therefore, there are levels with indexes from 0 to 2 in the table.
Other parameters from the response can also be briefly listed. The columns parameter indicates the number of columns, and their names are described in the header parameter. A value of 0 for the msgsRendered parameter indicates that the report template doesn't display tracks on the map. Empty stats and attachments parameters indicate that there are no Statistics nor attachments (e.g., charts) in the report template.
6. Selecting Rows in a Multi-Level Table
Knowing the index of the table content, let’s display it using the request report/select_result_rows.
https://hst-api.wialon.com/wialon/ajax.html?svc=report/select_result_rows¶ms={"tableIndex":0,"config":{"type":"range","data":{"from":0,"to":1,"level":2}}}&sid=SESSION_IDENTIFIER
Parameter and its value | Description |
---|---|
"tableIndex":0 | The content of the table with index 0 will be displayed. |
"type":"range" | A sequence of rows will be requested. |
"from":0 | The first displayed row will have index 0. |
"to":1 | The last displayed row will have index 1. |
"level":2 | The result will display nesting levels up to index 2. |
7. Export to File
Let’s export the result to a PDF file using the request report/export_result.
https://hst-api.wialon.com/wialon/ajax.html?svc=report/export_result¶ms={"format":2,"compress":0,"outputFileName":"Grouped%20trips"}&sid=SESSION_IDENTIFIER
Parameter and its value | Description |
---|---|
"format":2 | The report result will be exported in PDF format. |
"compress":0 | The exported file will not be compressed (added to an archive). |
"outputFileName":"Grouped%20trips" | The exported file will have the name Grouped trips. |
There is no response displayed for this API request, instead, the file download starts automatically.
8. Deleting the Result of the Previous Report
Use the request report/cleanup_result.
https://hst-api.wialon.com/wialon/ajax.html?svc=report/cleanup_result¶ms={}&sid=SESSION_IDENTIFIER
A zero value indicates successful deletion.
Peculiarities of Obtaining Results
Obtaining data from tables can be performed using the requests report/get_result_rows or report/select_result_rows, which use the parameter tableIndex to access a table with a specific index. At the same time, it is necessary to take into account that when executing a report for different intervals, the index of the same table may change due to the presence or absence of other tables.
To better understand the situation, let's consider an example. Let’s suppose the report template includes tables for Trips, Fuel Fillings, and Geofences. When executing the report for the interval from July 1st to 3rd, the result will contain all tables, and therefore their indexes will have the following values:
0 — Trips
1 — Fuel Drains
2 — Geofences
At the same time, when executing the report for the interval from July 4th to 6th, the indexes of some tables will change due to the absence of registered fuel fillings and will take other values:
0 — Trips
1 — Geofences
In this example, it is clear that when executing the report for different intervals, the index of the Geofences table changes. Therefore, accessing the table with index 2 will not always display information about visiting geofences. To fix such situations, it is recommended to apply additional checks, for example, based on the table name or the names of its columns.